package com.fbooks.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.fbooks.bean.Logger;
import com.fbooks.bean.User;
import com.fbooks.service.LoggerService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.nio.cs.US_ASCII;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: LuoRunFeng
 * @Email: 475403147@qq.com
 * @Time: 2020/6/14 0014 13:37
 * @Team: 蜂鸟工作室
 */
@Component
@Aspect
public class UserLogger {

    @Autowired
    private LoggerService loggerService;

    // joinPoint.getSignature().getDeclaringTypeName() 可以获取到调用方法的类名（包括包名），
    // joinPoint.getSignature().getName()    可以获取方法名，
    //Arrays.toString(joinPoint.getArgs())   得到的是方法调用的参数列表，
    //joinPoint.proceed()   可以得到方法的返回结果

    //  登录通知
    @Around("execution( * com.fbooks.Controller.UserController.userLogin(..))")
    public Object aroundLogForLogin(ProceedingJoinPoint joinpoint) {
        JSONPObject user = null;
        Logger logger = new Logger();
        Object result = null;
        try {
            logger.setInfo("用户登录操作："+JSON.parseObject(String.valueOf(joinpoint.getArgs()[0])).get("email"));

            long start = System.currentTimeMillis();
            //有返回参数 则需返回值
            result =  joinpoint.proceed();

            //判断注册是否成功
            String isSuccess = String.valueOf(JSON.parseObject(String.valueOf(result)).get("isSuccess"));
            logger.setIsSuccess(isSuccess);

            //获取原因
            String info = String.valueOf(JSON.parseObject(String.valueOf(result)).get("info"));
            logger.setReason(info);


            long end = System.currentTimeMillis();
            logger.setSpendTime("总共执行时长 " + (end - start) + " 毫秒");
            logger.setTime(new Timestamp(System.currentTimeMillis()));

            //写入日志
            loggerService.insertLogger(logger);
        } catch (Throwable t) {
            System.out.println("出现错误");
        }
        return result;
    }


    //  注册通知
    @Around("execution( * com.fbooks.Controller.UserController.insertUserInfo(..))")
    public Object aroundLogForRegister(ProceedingJoinPoint joinpoint) {
        JSONPObject user = null;
        Logger logger = new Logger();
        Object result = null;
        try {
            logger.setInfo("用户注册操作："+JSON.parseObject(String.valueOf(joinpoint.getArgs()[0])).get("email"));

            long start = System.currentTimeMillis();
            //有返回参数 则需返回值
            result =  joinpoint.proceed();

            //判断注册是否成功
            String isSuccess = String.valueOf(JSON.parseObject(String.valueOf(result)).get("isSuccess"));
            logger.setIsSuccess(isSuccess);

            //获取原因
            String info = String.valueOf(JSON.parseObject(String.valueOf(result)).get("info"));
            logger.setReason(info);


            long end = System.currentTimeMillis();
            logger.setSpendTime("总共执行时长 " + (end - start) + " 毫秒");
            logger.setTime(new Timestamp(System.currentTimeMillis()));

            //写入日志
            loggerService.insertLogger(logger);
        } catch (Throwable t) {
            System.out.println("出现错误");
        }
        return result;
    }


    //  注册通知
    @Around("execution( * com.fbooks.Controller.UserController.updateUserInfo(..))")
    public Object aroundLogForUpdateInfo(ProceedingJoinPoint joinpoint) {
        JSONPObject user = null;
        Logger logger = new Logger();
        Object result = null;
        try {
            logger.setInfo("用户信息修改："+JSON.parseObject(String.valueOf(joinpoint.getArgs()[0])).get("email"));

            long start = System.currentTimeMillis();
            //有返回参数 则需返回值
            result =  joinpoint.proceed();

            //判断注册是否成功
            String isSuccess = String.valueOf(JSON.parseObject(String.valueOf(result)).get("isSuccess"));
            logger.setIsSuccess(isSuccess);

            //获取原因
            String info = String.valueOf(JSON.parseObject(String.valueOf(result)).get("info"));
            logger.setReason(info);


            long end = System.currentTimeMillis();
            logger.setSpendTime("总共执行时长 " + (end - start) + " 毫秒");
            logger.setTime(new Timestamp(System.currentTimeMillis()));

            //写入日志
            loggerService.insertLogger(logger);
        } catch (Throwable t) {
            System.out.println("出现错误");
        }
        return result;
    }
}
